Shell脚本实战03-批量创建特殊要求用户

1. 批量创建特殊要求用户

批量创建10个系统账号theshu01-tehshu10并设置密码(密码为随机数,要求是字符和数字等的混合)

2. 参考答案1

开发思路如下:

1、创建10个系统账号,即theshu01-theshu10

对于给一个数字加0有多种实现方法,这里给出两种:

  1. seq -w 10

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@AmingLinux-107 shell]# seq -w 10
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
  2. echo {01..10}

    1
    2
    [root@AmingLinux-107 shell]# echo {01..10}
    01 02 03 04 05 06 07 08 09 10

2、要想通过脚本创建账号,必须知道如何实现无交互设置密码,如下:

1
2
3
4
[root@AmingLinux-107 shell]# useradd test
[root@AmingLinux-107 shell]# echo 1234556 | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。

3、密码为随机数,并且是8位字符串,这是个难点

实现随机数的方法很多,这里先给出常见的RANDOM方法,如下:

1
2
3
4
5
6
[root@AmingLinux-107 shell]# echo $RANDOM
7218
[root@AmingLinux-107 shell]# echo $RANDOM | md5sum
4d7a84ba085e17f2a7a643ae8740016b -
[root@AmingLinux-107 shell]# echo $RANDOM | md5sum | cut -c 5-12
ed80afc6

4、最后是实现相应的脚本。由于是批量创建10个账号并设置密码,因此需要使用for循环。

先来看一个带陷阱的错误解答方法:

1
2
3
4
5
6
7
8
#!/bin/bash
rm -f /tmp/user.log
for i in `seq -w 10`
do
useradd theshu$i && \
echo "echo $RANDOM | md5sum | cut -c 1-8" | passwd --stdin theshu$i
echo -e "user:theshu$i \t pass:`echo $RANDOM | md5sum | cut -c 1-8`" >> /tmp/user.log
done

上述脚本中用了两次随机数,如果不将其定义成变量,就会出现执行看起来相同的命令两次但是结果却不同的情况。

下面是一个输出不够美观但结果正确的答案:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
user="theshu"
passfile="/tmp/user.log"
for num in `seq -w 10`
do
useradd $user$num
pass="`echo "test$RANDOM" | md5sum | cut -c3-11 `"
echo "$pass" | passwd --stdin $user$num
echo -e "user:$user$num\tpasswd:$pass" >> $passfile
done
echo -------------ok------------
cat $passfile

下面的方法会调用系统函数库让输出更美观,同时增强逻辑性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
. /etc/init.d/functions
user="theshu"
passfile="/tmp/user.log"
for num in `seq -w 10`
do
pass="`echo "test$RANDOM" | md5sum | cut -c3-11`"
useradd $user$num &> /dev/null && \
echo "$pass" | passwd --stdin $user$num &> /dev/null & & \
echo -e "user:$user$num\tpassword:$pass" >> $passfile
if [ $? -eq 0 ]
then
action "$user$num is ok" /bin/true
else
action "$user$num is fail" /bin/false
fi
done
echo ------------------
cat $passfile && >$passfile

执行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
theshu01 is ok [ 确定 ]
theshu02 is ok [ 确定 ]
theshu03 is ok [ 确定 ]
theshu04 is ok [ 确定 ]
theshu05 is ok [ 确定 ]
theshu06 is ok [ 确定 ]
theshu07 is ok [ 确定 ]
theshu08 is ok [ 确定 ]
theshu09 is ok [ 确定 ]
theshu10 is ok [ 确定 ]
------------------
user:theshu01 password:fa30bd68a
user:theshu02 password:088f828bf
user:theshu03 password:357b72c78
user:theshu04 password:f57b1e123
user:theshu05 password:cfb279968
user:theshu06 password:d601af4e5
user:theshu07 password:82d46cea4
user:theshu08 password:29973c07b
user:theshu09 password:3079861de
user:theshu10 password:4f79cf007

注意:随机数的字符串要定义成变量,否则,每次执行结果都会不相同。

3. 参考答案2

1、按照参考答案1的思路正常创建账号

2、要批量创建密码,可使用chpasswd来实现,chpasswd是一个批量更新用户口令的工具。

chpasswd的使用示例:echo "test:123456" | chpasswd

给多个用户设置密码的命令为:chpasswd < 密码文件

但密码文件的内容必须以下面的格式来书写,并且不能有空行:

1
2
3
用户名1:口令1
用户名2:口令2
...

说明:用户必须存在

最后实现的脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
. /etc/init.d/functions
user="theshu"
passfile="/tmp/user.log"
for num in `seq -w 10`
do
pass="`echo "test$RANDOM" | md5sum | cut -c3-11`"
useradd $user${num} &> /dev/null &&\
echo -e "$user${num}:$pass" >> $passfile
if [ $? -eq 0 ]
then
action "$user${num} is ok" /bin/true
else
action "$user${num} is fail" /bin/false
fi
done
echo ------------
chpasswd < $passfile
cat $passfile && > $passfile

0%